﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Titan.FIS.LmsBalanceCollect
{
    public class frmHRDTestCRI
    {
    }
    public partial class frmLmsTest
    {
        public void TestCRIInit(ColumnRowMergeView columnRowMergeView, int devId, int heatTime, int drumTime)
        {
            columnRowMergeView._testType = "CRI";
            columnRowMergeView._heatTime = heatTime;
            columnRowMergeView._drumTime = drumTime;
            if (columnRowMergeView._dtTimers.Count > 0)
            {
                for (int i = 0; i < columnRowMergeView._dtTimers.Count; i++)
                {
                    if (columnRowMergeView._dtTimers[i + 1].timer.Enabled)
                        columnRowMergeView._dtTimers[i + 1].timer.Stop();
                }
                columnRowMergeView._dtTimers.Clear();
            }
            var timer1 = new Timer
            {
                Enabled = false,
                Interval = 1000,
                Tag = columnRowMergeView,
            };
            var timer2 = new Timer
            {
                Enabled = false,
                Interval = 1000,
                Tag = columnRowMergeView,
            };
            timer1.Tick += new System.EventHandler(this.As1timer1_Tick);
            timer2.Tick += new System.EventHandler(this.As1timer2_Tick);
            TimerConfig timerConfig1 = new TimerConfig();
            timerConfig1.timer = timer1;
            TimerConfig timerConfig2 = new TimerConfig();
            timerConfig2.timer = timer2;
            columnRowMergeView._dtTimers.Add(1, timerConfig1);
            columnRowMergeView._dtTimers.Add(2, timerConfig2);


            DataTable dt = new DataTable();
            dt.Columns.Add(" ");
            dt.Columns.Add("  ");
            dt.Columns.Add("   ");
            dt.Columns.Add("    ");
            dt.Rows.Add("化验编码", "", "设备编号", devId + 1 + "#");
            dt.Rows.Add("焦炭试样质量M(g)", "", "焦炭试样粒数", "");
            dt.Rows.Add("反应后残余焦炭质量M1(g)", "", "反应后残余焦炭粒数", "");
            dt.Rows.Add("转鼓后大于10mm粒数级焦炭质量M2(g)", "", "转鼓后大于10mm粒数级焦炭粒数", "");
            dt.Rows.Add("焦碳反应性CRI=M-M1/M*100(%)", "", "焦碳反应后强度CSR=M2/M1*100(%)", "");
            dt.Rows.Add("备注", "", "", "");
            columnRowMergeView.DataSource = dt;


            //设置读写和背景色  
            columnRowMergeView.Columns[0].ReadOnly = true;
            columnRowMergeView.Columns[2].ReadOnly = true;
            ////columnRowMergeView.Rows[0].Cells[3].ReadOnly = true;
            columnRowMergeView.Rows[1].Cells[1].ReadOnly = true;
            columnRowMergeView.Rows[2].Cells[1].ReadOnly = true;
            columnRowMergeView.Rows[2].Cells[3].ReadOnly = true;
            columnRowMergeView.Rows[3].Cells[1].ReadOnly = true;
            columnRowMergeView.Rows[3].Cells[3].ReadOnly = true;
            columnRowMergeView.Rows[4].ReadOnly = true;
            SetCellsBackColor(columnRowMergeView, 4, 4, 1, 1, Color.LightGray);
            SetCellsBackColor(columnRowMergeView, 4, 4, 3, 3, Color.LightGray);

            //合并单元格
            //columnRowMergeView1.MergeColNames.Add(new int[3] { 3, 0, 2 });
            columnRowMergeView.MergeRowNames.Add(new int[3] { 5, 1, 3 });
            columnRowMergeView.Rows[5].Cells[2].ReadOnly = true;
            columnRowMergeView.Rows[5].Cells[3].ReadOnly = true;

            SetDefault(columnRowMergeView);

            SetCellsTag(columnRowMergeView, 1, 1, 1, 1, true);
        }

        public void SetDefault(ColumnRowMergeView columnRowMergeView)
        {
            columnRowMergeView.ColumnHeadersHeight = 40;
            columnRowMergeView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            //自适应宽度
            foreach (DataGridViewColumn column in columnRowMergeView.Columns)
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                column.MinimumWidth = 200;
            }

            //自适应高度
            //columnRowMergeView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;

            //居中，不排序
            for (int a = 0; a < columnRowMergeView.Columns.Count; a++)
            {
                columnRowMergeView.Columns[a].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                columnRowMergeView.Columns[a].SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            //所有tag全部为false
            for (int i = 0; i < columnRowMergeView.Rows.Count; i++)
            {
                for (int a = 0; a < columnRowMergeView.Columns.Count; a++)
                {
                    columnRowMergeView.Rows[i].Cells[a].Tag = false;
                }
                //设置每行的高度为45
                columnRowMergeView.Rows[i].Height = 50;
            }
        }

        public void SetCellsTag(ColumnRowMergeView columnRowMergeView, int rowBegin, int rowEnd, int cellBegin, int cellEnd, bool tag)
        {
            for (int i = rowBegin; i <= rowEnd; i++)
            {
                for (int j = cellBegin; j <= cellEnd; j++)
                {
                    columnRowMergeView.Rows[i].Cells[j].Tag = tag;
                }
            }
        }

        public void SetCellsBackColor(ColumnRowMergeView columnRowMergeView, int rowBegin, int rowEnd, int cellBegin, int cellEnd, Color color)
        {
            for (int i = rowBegin; i <= rowEnd; i++)
            {
                for (int j = cellBegin; j <= cellEnd; j++)
                {
                    columnRowMergeView.Rows[i].Cells[j].Style.BackColor = color;
                }
            }
        }
    }
}
